package ${baseInfo.packageName};

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meibang.framework.common.utils.PageUtil;
import ${tableClass.fullClassName};
import ${serviceInterface.packageName}.${serviceInterface.fileName};
import ${mapperInterface.packageName}.${mapperInterface.fileName};
import ${addDTO.packageName}.*;
<#if baseService??&&baseService!="">
import ${baseService};
    <#list baseService?split(".") as simpleName>
        <#if !simpleName_has_next>
            <#assign serviceSimpleName>${simpleName}</#assign>
        </#if>
    </#list>
</#if>
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 *
 */
@Service
@Transactional(transactionManager = "primaryMybatisTransactionManager", rollbackFor = {RuntimeException.class, Exception.class})
public class ${baseInfo.fileName} extends ServiceImpl<${mapperInterface.fileName}, ${tableClass.shortClassName}> implements ${serviceInterface.fileName}{
    @Override
    public Long add(${tableClass.shortClassName}AddDTO addDTO) {
        // Step 1、规则校验
        // Step 1.1、 参数检验
        addDTO.paramCheck();
        // Step 1.2、业务规则校验,数据完整性约束校验，如唯一性、name不能重复等 businessCheck(addDTO);
        ${tableClass.shortClassName} aEntity = addDTO.buildEntity().entityCheck();

        // Step 2、保存到数据库
        baseMapper.insert(aEntity);
        return aEntity.getId();
    }

    @Override
    public void update(${tableClass.shortClassName}UpdateDTO updateDTO) {
        // Step 1、加载实体
        ${tableClass.shortClassName} aEntity = baseMapper.selectById(updateDTO.getId());

        // Step 2、更新实体
        aEntity.update(updateDTO);

        // Step 3、保存到数据库
        baseMapper.updateById(aEntity);
    }

    @Override
    public void deleteById(Long id) {
        if (Objects.isNull(id)) {
            return;
        }
        baseMapper.deleteById(id);
    }

    @Override
    public Page<${tableClass.shortClassName}${"VO"}> page(${tableClass.shortClassName}PageDTO conditions) {
        if (Objects.isNull(conditions)) {
            return new Page<${tableClass.shortClassName}${"VO"}>();
        }

        // Step 1、分页查询
        Page<${tableClass.shortClassName}> aPage = baseMapper.selectPage(
                    PageUtil.build(conditions),
                    new LambdaQueryWrapper<${tableClass.shortClassName}>().orderByDesc(${tableClass.shortClassName}::getId)
                );

        // Step 2、构造返回结果
        return PageUtil.convert(aPage, ${tableClass.shortClassName}VO::fromEntity);
    }

    @Override
    public List<${tableClass.shortClassName}${"VO"}> list(${tableClass.shortClassName}SearchDTO conditions) {
        if (Objects.isNull(conditions)) {
        return new ArrayList<>();
        }

        // Step 1、查询
        List<${tableClass.shortClassName}> aPageList = baseMapper.selectList(
                    new LambdaQueryWrapper<${tableClass.shortClassName}>().orderByDesc(${tableClass.shortClassName}::getId)
                );

        // Step 2、构造返回结果
        return Optional.ofNullable(aPageList).map(p -> p.stream().map(${tableClass.shortClassName}VO::fromEntity).collect(Collectors.toList())).orElse(new ArrayList<>());
    }
}